<template>
  <el-dialog title="请先选择检查类型" :visible.sync="show" :close-on-click-modal="false" width="500px" @closed="$refs.form.resetFields()">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="集群" prop="deptId1" key="deptId1" v-if="isJq">
        <el-select v-model="form.deptId1" placeholder="请选择集群" filterable :loading="dept1Loading" style="width: 100%">
          <el-option v-for="item in dept1List" :key="item.fid" :label="item.deptName" :value="item.fid" />
        </el-select>
      </el-form-item>
      <el-form-item label="检查类型" prop="bsNodeId">
        <el-select v-model="form.bsNodeId" placeholder="请选择检查类型" filterable :loading="listLoading" style="width: 100%">
          <el-option v-for="item in list" :key="item.bsNodeId" :label="item.businessType" :value="item.bsNodeId" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" @click="btnSubmit">确 定</el-button>
      <el-button @click="show = false">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
// 选择检查类型 (实际取 bsNodeId)
import { debounce } from '@/utils'
import { CQ_EVAL_RDC_TYPE as RDC_TYPE } from '@/utils/baseData'
import { listBsNode } from '@/api/cqPe/bsNode'
import { getDept1List } from '@/api/cqPe/peRecord'
import { handleableGenType } from './common'
export default {
  data() {
    return {
      show: false,
      form: {
        deptId1: '',
        bsNodeId: ''
      },
      rules: {
        deptId1: [{ required: true, message: '请选择集群' }],
        bsNodeId: [{ required: true, message: '请选择检查类型' }]
      },
      listLoading: false,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 9999,
        rdcType: '',
        belongSystem: '',
        // generationType: 'CHECK,CHOOSE', // 检查,抽查
        isEnable: true
      },
      dept1Loading: false,
      dept1List: [] // 集群
    }
  },
  computed: {
    isJq() {
      // 是否集群
      return this.queryParams.rdcType === RDC_TYPE.DEPT2
    }
  },
  created() {
    this.btnSubmit = debounce(this.onSubmit, 1e3, true)
  },
  methods: {
    open({ rdcType, belongSystem }) {
      this.show = true
      this.queryParams.rdcType = rdcType || ''
      this.queryParams.belongSystem = belongSystem || ''
      this.getList()

      this.form.deptId1 = ''
      if (this.isJq) this.getDept1List()
    },
    async getDept1List() {
      this.dept1Loading = true
      const res = await getDept1List().catch(() => false)
      this.dept1Loading = false
      if (res === false) return

      const data = res.data || []
      this.dept1List = data
      this.form.deptId1 = data[0]?.fid || ''
    },
    async getList() {
      this.listLoading = true
      const res = await listBsNode(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      let data = res.rows || []
      // 只取 检查,抽查
      data = data.filter(i => handleableGenType.includes(i.generationType))
      this.list = data
    },
    async onSubmit() {
      const valid = await this.$refs.form.validate().catch(() => false)
      if (!valid) return

      const { deptId1, bsNodeId } = this.form
      const item = this.list.find(i => i.bsNodeId === bsNodeId)
      const data = { ...item, deptId1 }
      this.$emit('ok', data)
      this.show = false
    }
  }
}
</script>
